package com.bestcem.xm.ticket.mq;//package com.bestcem.xm.ticket.mq;
//
//import com.alibaba.fastjson.JSON;
//import com.bestcem.xm.ticket.dao.TicketDao;
//import com.bestcem.xm.ticket.entity.mongo.Ticket;
//import com.bestcem.xm.ticket.enums.TicketEventEnum;
//import com.bestcem.xm.ticket.service.dto.mq.FollowOverdueDTO;
//import com.bestcem.xm.ticket.service.dto.mq.TicketSendDTO;
//import com.bestcem.xm.ticket.util.fsm.TicketFsmUtil;
//import com.bestcem.xm.ticket.util.record.param.TransferParam;
//import com.bestcem.xm.ticket.task.NotificationTask;
//import com.bestcem.xm.util.MsgPackFormatUtil;
//import com.rabbitmq.client.Channel;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.annotation.RabbitHandler;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.util.Base64;
//import java.util.Objects;
//
///**
// * 工单延迟队列MQ监听
// *
// * @author XiaoYunSong <ys.xiao@idiaoyan.com>
// * @version 1.0
// * @date 2021/9/16 15:03
// */
//@Slf4j
//@Component
//public class TicketDelayMqReceive {
//    @Resource
//    private TicketDao ticketDao;
//
//    @Resource
//    private TicketFsmUtil ticketFsmUtil;
//
//    @Resource
//    private NotificationTask notificationTask;
//
//    /**
//     * 监听 工单延迟队列MQ
//     */
//    @RabbitListener(queues = "#{ticketDelayQueue.name}", concurrency = "${app.ticket.rabbitmq.ticketDelayQueue.concurrency:10}")
//    @RabbitHandler
//    public void handlerApp(Message dto, Channel channel) {
//
//        try {
//            if (log.isInfoEnabled()) {
//                log.info("[Ticket] 工单延迟队列收到消息:{}", Base64.getEncoder().encodeToString(dto.getBody()));
//            }
//
//
//            MsgPackFormatUtil.MqMessage mqMessage = MsgPackFormatUtil.mqDecode(dto.getBody());
//            if (mqMessage == null) {
//                log.error("[Ticket] 工单延迟消息解析失败:" + Base64.getEncoder().encodeToString(dto.getBody()));
//                channel.basicNack(dto.getMessageProperties().getDeliveryTag(), false, false);
//                return;
//            }
//            if (log.isInfoEnabled()) {
//                log.info("[Ticket] 工单延迟队列消息体 {}", JSON.toJSONString(mqMessage));
//            }
//            // 接收 计算周期行任务通知 提醒消息
//            if (TicketDelayMqSender.TICKET_SEND_NOTIFY_TOPIC.equals(mqMessage.getTopic())) {
//                TicketSendDTO ticketSendDTO = MsgPackFormatUtil.mapToObj(mqMessage.getData(), TicketSendDTO.class);
//                if (log.isInfoEnabled()) {
//                        log.info("[Ticket] 计算周期行任务通知提醒消息 {}", JSON.toJSONString(ticketSendDTO));
//                }
//                notificationTask.receiveNotifyMq(ticketSendDTO);
//            }
//
//            // 接收 工单流转 提醒消息
//            if (TicketDelayMqSender.TICKET_TICKET_OVERDUE_TOPIC.equals(mqMessage.getTopic())) {
//                FollowOverdueDTO followOverdueDTO = MsgPackFormatUtil.mapToObj(mqMessage.getData(), FollowOverdueDTO.class);
//                Ticket ticket = ticketDao.findById(followOverdueDTO.getTicketId());
//                if (Objects.isNull(ticket)) {
//                    log.error("[Ticket] 工单逾期消息消费失败, 工单查询为空; TicketId:{}", followOverdueDTO.getTicketId());
//                    channel.basicAck(dto.getMessageProperties().getDeliveryTag(), false);
//                    return;
//                }
//                if (Objects.isNull(ticket.getFollowDeadline())) {
//                    if (log.isInfoEnabled()) {
//                        log.info("[Ticket] 工单逾期消息消费跳过 工单被更新逾期时间已被清除");
//                    }
//                } else {
//                    ticketFsmUtil.transfer(ticket, TicketEventEnum.getByIndex(followOverdueDTO.getEvent()), new TransferParam());
//                }
//            }
//
//            channel.basicAck(dto.getMessageProperties().getDeliveryTag(), false);
//        } catch (Exception e) {
//            String errorMsg = Base64.getEncoder().encodeToString(dto.getBody());
//            log.error("[Ticket] 工单延迟队列消息处理异常 {}", errorMsg, e);
//            try {
//                channel.basicNack(dto.getMessageProperties().getDeliveryTag(), false, false);
//            } catch (IOException ex) {
//                log.error("[Ticket]: 工单延迟队列消息确认失败", ex);
//            }
//        }
//    }
//}
